package com.wash.shoes.controller.mobile;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.wash.shoes.domain.vo.CustomerCouponVo;
import com.wash.shoes.entity.CouponPo;
import com.wash.shoes.entity.CustomerCouponPo;
import com.wash.shoes.exception.WashException;
import com.wash.shoes.service.CouponService;
import com.wash.shoes.service.CustomerCouponService;
import com.wash.shoes.util.WsRandomUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ljg
 * @since 2024-12-01
 */
@Api(tags = {"移动端-用户的优惠卷"})
@RestController
@RequestMapping("/mobile-api/customerCoupon")
public class CustomerCouponMController extends BaseMController {

    @Autowired
    CustomerCouponService customerCouponService;

    @Autowired
    CouponService couponService;

    @ApiOperation("获取优惠卷列表")
    @PostMapping("/list")
    public R<Page<CustomerCouponVo>> pageList(@RequestBody CustomerCouponVo customerCouponVo) {
        Page<CustomerCouponVo> page = customerCouponService.pageList(customerCouponVo);
        return R.ok(page);
    }

    @ApiOperation("用户获取优惠卷")
    @PostMapping("/saveUpdate")
    public synchronized R<CustomerCouponPo> add(@RequestBody CustomerCouponPo customerCouponPo) {
        //领取优惠卷需要检查是不是一个门店发出来的优惠卷
        LambdaQueryWrapper<CouponPo> query = new LambdaQueryWrapper<>();
        query.eq(CouponPo::getId, customerCouponPo.getCouponId());
/* wxy说抖音先不搞优惠卷直接先把这个注释掉,优惠卷不区分渠道       if (StringUtils.isNoneBlank(customerCouponPo.getChannel())) {
            query.eq(StringUtils.isNotEmpty(HeaderUtil.getPoId()), CouponPo::getPoId, HeaderUtil.getPoId());
        }*/
//        customerCouponPo.getCouponId();
        CouponPo couponPo = couponService.getOne(query);
        if (ObjectUtil.isNull(couponPo)) {
            throw new WashException("优惠券不存在");
        }
        // 判断是否超过限制
        List<CustomerCouponPo> couponPoList = customerCouponService.getUserCouponCount(customerCouponPo.getCustomerId(), customerCouponPo.getCouponId());
        if (couponPoList.size() >= couponPo.getPerLimit() && couponPo.getPerLimit() != 0) {
            throw new WashException("超出领取限制");
        }

        if (couponPo.getPublishCount() < couponPo.getReceiveCount()) {
            throw new WashException("该券已经被领取完");
        }

        // 优惠卷的领取+1
        couponPo.setReceiveCount(couponPo.getReceiveCount() + 1);
        couponService.updateById(couponPo);

        // 添加优惠卷
        CustomerCouponPo add = new CustomerCouponPo();
        add.setCustomerId(customerCouponPo.getCustomerId());
        add.setCouponId(customerCouponPo.getCouponId());
        add.setCouponNo("WS" + WsRandomUtil.generateByRandom(10));
        add.setChannel(customerCouponPo.getChannel());
        add.setName(couponPo.getName());
        add.setAmount(couponPo.getAmount());
        add.setFullMoney(couponPo.getFullMoney());
        customerCouponService.save(add);

        return R.ok(add);
    }


    @ApiOperation("获取详情")
    @PostMapping("/getDetailById")
    public R<CustomerCouponPo> getDetailById(String theId) {
        CustomerCouponPo customerCouponPo = customerCouponService.getById(theId);
        return R.ok(customerCouponPo);
    }

}

